package cn.sciento.farm.devicemanager.service.impl;

import cn.sciento.farm.devicemanager.dao.entity.DevDevice;
import cn.sciento.farm.devicemanager.dao.entity.DevStrategyLog;
import cn.sciento.farm.devicemanager.dao.mapper.StrategyLogMapper;
import cn.sciento.farm.devicemanager.dao.model.PageConditionQuery;
import cn.sciento.farm.devicemanager.service.DeviceService;
import cn.sciento.farm.devicemanager.service.StrategyLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Walton
 * @since 2020/10/15 17:14
 */
@Service
public class StrategyLogServiceImpl implements StrategyLogService {

    @Autowired
    private StrategyLogMapper strategyLogMapper;

    @Autowired
    private DeviceService deviceService;

    @Override
    public int insert(DevStrategyLog strategyLog) {
        return strategyLogMapper.insert(strategyLog);
    }

    @Override
    public IPage<DevStrategyLog> getStrategyLog(PageConditionQuery data) {
        QueryWrapper<DevStrategyLog> queryWrapper = new QueryWrapper<>();
        List<DevDevice> byAreaId = deviceService.getByAreaId(data.getAreaId());
        if (data.getControllerId() != 0) {
            queryWrapper.eq("controller_id", data.getControllerId());
        }
        if (byAreaId.size() < 1) {
            return null;
        }
        queryWrapper.ge("start_time",data.getBeginTime());
        queryWrapper.le("start_time",data.getEndTime());
        queryWrapper.orderByDesc("start_time");
        IPage<DevStrategyLog> logPage = strategyLogMapper.selectPage(new Page<>(data.getCurrentPage(), data.getPageSize()), queryWrapper);
        return logPage;
    }

    @Override
    public int batchDeleteLogByStartTime(long startTime) {
        return strategyLogMapper.batchDeleteLogByStartTime(startTime);
    }
}
